/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package researchgrants.parts.GrantRequest.filters;

import researchgrants.parts.GrantRequest.*;
import java.util.Iterator;
import researchgrants.parts.GrantRequest.agency.Agency;
import researchgrants.parts.GrantRequest.agency.AgencyCategory;
import researchgrants.parts.GrantRequest.agency.AgencyContact;
import researchgrants.parts.GrantRequest.grantOfficer.GrantOfficer;
import researchgrants.parts.GrantRequest.statusChanges.GrantRequestStatusChange;
import researchgrants.parts.date.LoggedDate;
import researchgrants.parts.GrantRequest.expensePurpose.ExpensePurpose;
import researchgrants.parts.GrantRequest.nsfcode.NsfCode;
import researchgrants.parts.pi.Pi;
import researchgrants.parts.pi.PisList;

/**
 * Filter a GrantRequest if one of its fields contain the text given
 * @author DOStudent1
 */
class GrantRequestFilterFreeText implements GrantRequestFilter {

    // the content to search, lower cased
    private String freeText;
    
    GrantRequestFilterFreeText(String freeText) {
        this.freeText=freeText.toLowerCase();
    }

    // TODO: replace the different searches to use other filters
    
    @Override
    public boolean isFiltered(GrantRequest grantRequest) {
        boolean found=false;

        
        final LoggedDate deadline = grantRequest.getCurrentDeadline();
        if (deadline!=null) {
            if (deadline.toString().toLowerCase().contains(freeText)) {
                found=true;
            }
        }

        
        
        
        final LoggedDate firstContact = grantRequest.getCurrentFirstContact();
        if (firstContact!=null) {
            if (firstContact.toString().toLowerCase().contains(freeText)) {
                found=true;
            }
        }

        
        
        ExpensePurpose expensePurpose=grantRequest.getCurrentExpensePurpose();
        if (expensePurpose!=null) {
            if (expensePurpose.getExpensePurposeName().contains(freeText)) {
                found=true;
            } 
        }
        
        GrantOfficer grantOfficer=grantRequest.getCurrentGrantOfficer();
        if (grantOfficer!=null) {
            if (grantOfficer.getName().toLowerCase().contains(freeText)) {
                found=true;
            }
        }
        
        
        
        
        if (grantRequest.getCurrentNotes().toLowerCase().contains(freeText)) {
            found=true;
        }
        
        
        
        
        PisList pisList = grantRequest.getCurrentPis();
        for (Iterator<Pi> it = pisList.iterator(); it.hasNext() && !found;) {
            Pi pi = it.next();
            if (pi.getCurrentName().toLowerCase().contains(freeText)) {
                found=true;
            }
            if (pi.getCurrentNotes().toLowerCase().contains(freeText)) {
                found=true;
            }
            if (pi.getCurrentEmail().toLowerCase().contains(freeText)) {
                found=true;
            }
            if (pi.getCurrentTelephone().toLowerCase().contains(freeText)) {
                found=true;
            }
        }
        
        
        
        if (GrantRequestStatusChange.getStatusChangeTitle(grantRequest.getLastStatusChange()).toLowerCase().contains(freeText)) {
            found=true;
        }
        
        
        
        AgencyContact agencyContact=grantRequest.getCurrentAgencyContact();
        if (agencyContact!=null) {
            if (agencyContact.getCurrentAddress().toLowerCase().contains(freeText)) {
                found=true;
            }

            if (agencyContact.getCurrentEmail().toLowerCase().contains(freeText)) {
                found=true;
            }

            if (agencyContact.getCurrentName().toLowerCase().contains(freeText)) {
                found=true;
            }

            if (agencyContact.getCurrentNotes().toLowerCase().contains(freeText)) {
                found=true;
            }

            if (agencyContact.getCurrentPhone().toLowerCase().contains(freeText)) {
                found=true;
            }

            Agency agency=agencyContact.getCurrentAgency();
            if (agency.getCurrentAgencyName().toLowerCase().contains(freeText)) {
                found=true;
            }

            if (agency.getCurrentAgencyNotes().toLowerCase().contains(freeText)) {
                found=true;
            }

            AgencyCategory agencyCategory=agency.getCurrentAgencyCategory();
            if (agencyCategory.getAgencyCategoryNameString().toLowerCase().contains(freeText)) {
                found=true;
            }

            if (agencyCategory.getDescription().toLowerCase().contains(freeText)) {
                found=true;
            }
        }        
        
        
        
        NsfCode nsfCode=grantRequest.getCurrentNsfCode();
        if (nsfCode!=null) {
            if (nsfCode.getNsfCodeName().toLowerCase().contains(freeText)) {
                found=true;
            }
        }

        
        
        if (grantRequest.getCurrentTitle().toLowerCase().contains(freeText)) {
            found=true;
        }
        
        return(!found);
    }

}
